package DAO.impl;

import java.util.Date;

import javax.persistence.Query;

import DAO.HibernateUtil;
import DAO.PassengerDAO;

import model.impl.Passenger;


public class PassengerDAOImpl extends GenericDAOImpl<Passenger, Integer> implements
		PassengerDAO {

	public Passenger findPassengerByNameData(String name, String surname, Date data) {
		Passenger passenger = null;
		try {
			HibernateUtil.beginTransaction();
			String sqlRequest = "SELECT p FROM Passenger p WHERE p.PassengerName = :name AND p.PassengerSurname = :surname AND p.PassengerDateOfBirth = :data";
			Query query = HibernateUtil.getSession().createQuery(sqlRequest).setParameter("name", name).setParameter("surname", surname).setParameter("data", data);
			passenger = findOne(query);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackTransaction();
			e.printStackTrace();
            System.out.println("Error in findPassengerByNameSurnameDate");
        }
		return passenger;
	}

	public Passenger findPassengerByLogin(String login) {
		Passenger passenger = null;
		try {
			HibernateUtil.beginTransaction();
			String sqlRequest = "SELECT p FROM Passenger p WHERE p.login = :log";
			Query query = HibernateUtil.getSession().createQuery(sqlRequest).setParameter("log", login);
			passenger = findOne(query);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackTransaction();
			e.printStackTrace();
            System.out.println("Error infindPassengerByLogin");
		}
		return passenger;
	}

	public Passenger findPassengerByLoginPassword(String login, String password) {
		Passenger passenger = null;
		try {
			HibernateUtil.beginTransaction();
			String sqlRequest = "SELECT p FROM Passenger p WHERE p.login = :log AND p.password = :pas";
			Query query = HibernateUtil.getSession().createQuery(sqlRequest).setParameter("log", login).setParameter("pas", password);
			passenger = findOne(query);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackTransaction();
			e.printStackTrace();
            System.out.println("Error infindPassengerByLoginPassword");
		}
		return passenger;
	}
}
